import React, { PureComponent, Fragment } from 'react';
import { Card, Col, Form, Button, Input, Modal, message, Row, Steps } from 'antd';
import { connect } from 'dva';
const FormItem = Form.Item;
const { TextArea } = Input;
const { Step } = Steps;
@Form.create()
@connect()
class RepairAddForm extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    };
    this.user_id = sessionStorage.getItem('user_id');
    this.dispatch = this.props.dispatch;
  }
  submitEditValues = () => {
    const hide = message.loading('正在编辑...', 0);
    this.props.form.validateFields((err, values) => {
      if (!err) {
        const editObj = JSON.parse(JSON.stringify(this.props.editObj));
        delete editObj['CurrentNodeName'];
        // debugger
        if (this.props.editObj.CurrentNode == 5) {
          editObj.CheckContent = values.CheckContent;
          editObj.CurrentNode = 6;
        }
        if (this.props.editObj.CurrentNode == 4) {
          editObj.AcceptContent = values.AcceptContent;
          editObj.CurrentNode = 5;
        }
        if (this.props.editObj.CurrentNode == 3) {
          editObj.CompleteContent = values.CompleteContent;
          editObj.CompleteRemark = values.CompleteRemark;
          editObj.CurrentNode = 4;
        }
        if (this.props.editObj.CurrentNode == 2) {
          editObj.InspectContent = values.InspectContent;
          editObj.CurrentNode = 3;
        }
        if (this.props.editObj.CurrentNode == 1) {
          editObj.ApproveAdvice = values.ApproveAdvice;
          editObj.CurrentNode = 2;
        }

        this.dispatch({
          type: 'repair/fetchEditEquipment',
          payload: {
            // userID:this.user_id,
            userID: 'BFBAAD3E-679F-426F-BF03-05DEE138DBA6',
            model: editObj,
          },
          callback: response => {
            hide();
            if (response.status) {
              message.success('编辑成功');
              this.setState({ visible: false });
              this.props.queryList();
            } else {
              message.error('编辑失败');
              console.log(response.ErrorMessage);
            }
          },
        });
      }
    });
  };
  componentDidMount() {
    this.props.OnRef(this, { type: 'EditForm' });
  }
  render() {
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 10 },
      },
    };
    return (
      <Modal
        title="编辑"
        width={'60%'}
        destroyOnClose
        visible={this.state.visible}
        onOk={() => {
          this.submitEditValues();
        }}
        onCancel={() => {
          this.setState({ visible: false });
        }}
      >
        <Steps current={this.props.editObj.CurrentNode} size="small">
          <Step title="维修申请" />
          <Step title="维修审批" description="审批意见必须填写" />
          <Step title="维修抽查" description="抽查情况必须填写" />
          <Step title="维修上报" />
          <Step title="维修验收" description="验收意见必须填写" />
          <Step title="维修检查" description="检查情况必须填写" />
        </Steps>
        <Card style={{ margin: '5px 0' }}>
          <Form labelAlign="right">
            {this.props.editObj.CurrentNode == 1 ? (
              <FormItem label="处理意见" {...formItemLayout}>
                {getFieldDecorator('ApproveAdvice', {
                  rules: [{ required: true, message: '请填写处理意见！' }],
                  initialValue: this.props.editObj.ApproveAdvice,
                })(<TextArea placeholder="处理意见" rows={10} />)}
              </FormItem>
            ) : null}
            {this.props.editObj.CurrentNode == 2 ? (
              <FormItem label="中间验收抽查内容" {...formItemLayout}>
                {getFieldDecorator('InspectContent', {
                  rules: [{ required: true, message: '请填写中间验收抽查内容！' }],
                  initialValue: this.props.editObj.InspectContent,
                })(<TextArea placeholder="中间验收抽查内容" rows={10} />)}
              </FormItem>
            ) : null}

            {this.props.editObj.CurrentNode == 3 ? (
              <Fragment>
                <FormItem label="完成情况" {...formItemLayout}>
                  {getFieldDecorator('CompleteContent', {
                    initialValue: this.props.editObj.CompleteContent,
                  })(<TextArea placeholder="完成情况" rows={10} />)}
                </FormItem>
                <FormItem label="备注" {...formItemLayout}>
                  {getFieldDecorator('CompleteRemark', {
                    initialValue: this.props.editObj.CompleteContent,
                  })(<TextArea placeholder="备注" rows={10} />)}
                </FormItem>
              </Fragment>
            ) : null}
            {this.props.editObj.CurrentNode == 4 ? (
              <FormItem label="监理单位验收意见" {...formItemLayout}>
                {getFieldDecorator('AcceptContent', {
                  rules: [{ required: true, message: '请填写监理单位验收意见！' }],
                  initialValue: this.props.editObj.AcceptContent,
                })(<TextArea placeholder="监理单位验收意见" rows={10} />)}
              </FormItem>
            ) : null}
            {this.props.editObj.CurrentNode == 5 ? (
              <FormItem label="管理部门检查情况" {...formItemLayout}>
                {getFieldDecorator('CheckContent', {
                  rules: [{ required: true, message: '请填写管理部门检查情况！' }],
                  initialValue: this.props.editObj.CheckContent,
                })(<TextArea placeholder="管理部门检查情况" rows={10} />)}
              </FormItem>
            ) : null}
          </Form>
        </Card>
      </Modal>
    );
  }
}

export default RepairAddForm;
